/* Ledger.h
Michael Zahniser, 2008

Header file for the Ledger class, which represents a tree of account
subtotals.
*/ 

#ifndef __MZ_LEDGER_H__
#define __MZ_LEDGER_H__

#include "_ncurses/TextTree.h"

#include <map>
#include <string>
#include <ostream>
#include <list>
#include <vector>

using std::map;
using std::string;
using std::ostream;
using std::list;
using std::vector;



class Ledger {
public:
    // Add a line to the ledger. The account name should be a series
    // of token separated by " :: ". Each token will be made into a
    // separate node of the tree containing a subtotal.
    void Add(string accountName, double balance);

    // Get the total of all accounts in the ledger.
    double Total() const;

    // Print the ledger. If a total is specified, balances are shown both
    // as actual amounts and as percentages of the total.
    void Print(ostream &out, double total = 0.0) const;
    void Print(TextTree &tree, double total = 0.0) const;


private:
    class Line {
    public:
	Line(string name, double balance, int indent)
	    : name(name), balance(balance), indent(indent) {}
	
	string name;
	double balance;
	int indent;
    };


private:
    list<Line> lines;
    // These are all the ancestors of the most recently added tree node.
    vector<list<Line>::iterator> stack;
};


    
#endif
